<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 用户资金管理模块
 */
class Finance extends Admin_Controller {
	
	function __construct()
	{
	    parent::__construct();
            $this->load->model('users_model');
            $this->load->model('account_model');
            $this->load->model('finance_list_model');
            $this->load->model('experience_list_model');
            $this->load->model('withdraw_list_model');
            $this->load->model('exchange_list_model');
            //奖励类型
            $this->award_type_num = array('1'=>'reg_award','2'=>'task_award','3'=>'exchange_award','4'=>'sign_award','5'=>'withdraw_award','6'=>'spread_award','7'=>'exper_award','8'=>'other_award','9'=>'withdraw_deduct','10'=>'exchange_deduct');
            $this->award_type_title = array('1'=>'注册奖励','2'=>'任务奖励','3'=>'兑换奖励','4'=>'签到奖励','5'=>'提现奖励','6'=>'推广奖励','7'=>'体验奖励','8'=>'其他奖励','9'=>'提现扣除','10'=>'兑换扣除');
            $this->award_title = array('reg_award'=>'注册奖励','task_award'=>'任务奖励','exchange_award'=>'兑换奖励','sign_award'=>'签到奖励','withdraw_award'=>'提现奖励','spread_award'=>'推广奖励','exper_award'=>'体验奖励','other_award'=>'其他奖励','withdraw_deduct'=>'提现扣除','exchange_deduct'=>'兑换扣除');
	}
        
	public function index()
	{	
            $where = array();
            
            if(isset($_GET['key']) && $_GET['key']){
                $keyword = trim($_GET['key']);
                if(preg_match('/^\d+$/i', $keyword)){
                    $user = $this->users_model->get_user_by_uid($keyword);
                }else{
                    $user = $this->users_model->get_user_by_username($keyword);
                }
                if($user){
                    $where['account.user_id'] = $user['user_id'];
                }
            }
            //总数
            $total = $this->account_model->get_count($where);
            $navigation_position = 'outside';
            $records_per_page = 10;
            $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position,
                array('left', 'right')) ? $navigation_position : 'outside');
            //$this->xpage->reverse(true);
            $this->xpage->records($total);
            $this->xpage->records_per_page($records_per_page);
            $this->xpage->labels("<<", ">>");
            //记录
            $list = $this->account_model->get_list($where, (($this->xpage->get_page() - 1) * $records_per_page),
                $records_per_page);
            
            $T_total = $this->account_model->get_total_count($where);
            
            $this->template->assign('keyword', $keyword);
            $this->template->assign('T_total', $T_total);
            $this->template->assign('Total', $total);
            $this->template->assign('showpage', $this->xpage->render(true));
            $this->template->assign('list', $list);
            $this->template->load('finance/index');
	}
        //溜溜币明细
        public function finance1(){
            $where = array();
            $where_in = array();
            //变动类型
            if(isset($_GET['stm']) && $_GET['stm']){
                $where['type'] = intval($_GET['stm']);
                $type = $_GET['stm'] == 1 ? '收入' : '支出';
            }
            //奖励类型
            if(isset($_GET['at']) && $_GET['at']){
                $award_ = $this->award_type_num[$_GET['at']];
                $award_title = $this->award_type_title[$_GET['at']];
                $where['reward_type'] = $award_;
            }
            //用户类型
            if(isset($_GET['st']) && isset($_GET['q']) && $_GET['q']){
                $user_type = $_GET['st'];
                $user_title = $user_type == 1 ? '用户名' : '推荐人';
                $keyword = trim($_GET['q']);
                $user = $this->users_model->get_user_by_username($keyword);
                if($user){
                    if($user_type == 1){
                        $where['finance_list.user_id'] = $user['user_id'];
                    }else{
                        $ch = $this->users_model->get_spreaders_id(array('parent_id'=>$user['user_id']));
                        if($ch){
                            foreach ($ch as $key => $val){
                                if($key == 0){
                                    $str.=$val['user_id'];
                                }else{
                                    $str.=','.$val['user_id'];
                                }
                            }
                            
                            if($str){
//                                $where_in = 'finance_list.user_id in('.$str.')';
                                $where_in = $str;
                            }
                        }
                    }
                }
            }
            
            if(isset($_GET['uname']) && $_GET['uname']){
                $u = $this->users_model->get_user_by_username($_GET['uname']);
                if($u) {$where['finance_list.user_id'] = $u['user_id'];}
            }
            
            
            if($_GET['time-start']){
                $where['create_time >='] = strtotime($_GET['time-start']);
            }
            if($_GET['time-end']){
                $where['create_time <='] = strtotime($_GET['time-end']);
            }
            
            $keyword = $_GET['uname'] ? $_GET['uname'] : $keyword;
            
            //总数
            $total = $this->finance_list_model->get_count($where,$where_in);
            $navigation_position = 'outside';
            $records_per_page = 10;
            $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position,
                array('left', 'right')) ? $navigation_position : 'outside');
            //$this->xpage->reverse(true);
            $this->xpage->records($total);
            $this->xpage->records_per_page($records_per_page);
            $this->xpage->labels("<<", ">>");
            //记录
            $list = $this->finance_list_model->get_list($where, (($this->xpage->get_page() - 1) * $records_per_page),
                $records_per_page,$where_in);
            foreach ($list as $key => $val){
                $spreader = $this->users_model->get_user_by_uid(intval($val['parent_id']));
                $list[$key]['spread'] = $spreader ? $spreader['username'] : '';
                $list[$key]['type'] = $val['type'] == 1 ? '收入' : '支出';
                $list[$key]['reward_type'] = $val['reward_type'] ? $this->award_title[$val['reward_type']] : "其他奖励";
                $list[$key]['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
                if($val['friend_id']){
                    $friend = $this->users_model->get_user_by_uid($val['friend_id']);
                    $list[$key]['friend_name'] = $friend['username'];
                }
            }
            $T_total = $this->finance_list_model->get_total_count($where,$where_in);
            $this->template->assign('name', $keyword);
            $this->template->assign('cont1', $type);
            $this->template->assign('award', $award_title);
            $this->template->assign('cont', $user_title);
            $this->template->assign('T_total', $T_total);
            $this->template->assign('Total', $total);
            $this->template->assign('time_start', $_GET['time-start']);
            $this->template->assign('time_end', $_GET['time-end']);
            $this->template->assign('showpage', $this->xpage->render(true));
            $this->template->assign('list', $list);
            $this->template->load('finance/finance');
        }
        
        //积分明细
        public function experience(){
            $where = array();
            $where_in = array();
            //变动类型
            if(isset($_GET['stm']) && $_GET['stm']){
                $where['type'] = intval($_GET['stm']);
                $type = $_GET['stm'] == 1 ? '收入' : '支出';
            }
            //奖励类型
            if(isset($_GET['at']) && $_GET['at']){
                $award_ = $this->award_type_num[$_GET['at']];
                $award_title = $this->award_type_title[$_GET['at']];
                $where['reward_type'] = $award_;
            }
            //用户类型
            if(isset($_GET['st']) && isset($_GET['q']) && $_GET['q']){
                $user_type = $_GET['st'];
                $user_title = $user_type == 1 ? '用户名' : '推荐人';
                $keyword = trim($_GET['q']);
                $user = $this->users_model->get_user_by_username($keyword);
                if($user){
                    if($user_type == 1){
                        $where['experience_list.user_id'] = $user['user_id'];
                    }else{
                        $ch = $this->users_model->get_spreaders_id(array('parent_id'=>$user['user_id']));
                        if($ch){
                            foreach ($ch as $key => $val){
                                if($key == 0){
                                    $str.=$val['user_id'];
                                }else{
                                    $str.=','.$val['user_id'];
                                }
                            }
                            
                            if($str){
//                                $where_in = 'finance_list.user_id in('.$str.')';
                                $where_in = $str;
                            }
                        }
                    }
                }
            }
            
            $keyword = $_GET['uname'] ? $_GET['uname'] : $keyword;
            
            if($_GET['time-start']){
                $where['create_time >='] = strtotime($_GET['time-start']);
            }
            if($_GET['time-end']){
                $where['create_time <='] = strtotime($_GET['time-end']);
            }
            
            if(isset($_GET['uname']) && $_GET['uname']){
                $u = $this->users_model->get_user_by_username($_GET['uname']);
                if($u) $where['experience_list.user_id'] = $u['user_id'];
            }
            
            //总数
            $total = $this->experience_list_model->get_count($where,$where_in);
            $navigation_position = 'outside';
            $records_per_page = 10;
            $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position,
                array('left', 'right')) ? $navigation_position : 'outside');
            //$this->xpage->reverse(true);
            $this->xpage->records($total);
            $this->xpage->records_per_page($records_per_page);
            $this->xpage->labels("<<", ">>");
            //记录
            $list = $this->experience_list_model->get_list($where, (($this->xpage->get_page() - 1) * $records_per_page),
                $records_per_page,$where_in);
            foreach ($list as $key => $val){
                $spreader = $this->users_model->get_user_by_uid(intval($val['parent_id']));
                $list[$key]['spread'] = $spreader ? $spreader['username'] : '';
                $list[$key]['type'] = $val['type'] == 1 ? '收入' : '支出';
                $list[$key]['reward_type'] = $val['reward_type'] ? $this->award_title[$val['reward_type']] : "其他奖励";
                $list[$key]['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
            }
            $T_total = $this->experience_list_model->get_total_count($where,$where_in);
            $this->template->assign('name', $keyword);
            $this->template->assign('cont1', $type);
            $this->template->assign('award', $award_title);
            $this->template->assign('cont', $user_title);
            $this->template->assign('T_total', $T_total);
            $this->template->assign('Total', $total);
            $this->template->assign('time_start', $_GET['time-start']);
            $this->template->assign('time_end', $_GET['time-end']);
            $this->template->assign('showpage', $this->xpage->render(true));
            $this->template->assign('list', $list);
            $this->template->load('finance/experience');
        }
        //提现明细
        public function withdraw(){
            $where = array();
            
            //变动类型
            if(isset($_GET['stm']) && $_GET['stm']){
                $where['withdraw_list.status'] = intval($_GET['stm']);
                $type = $_GET['stm'] == 1 ? '已支付' : ($_GET['stm'] == 2 ? '未支付' : '失败');
            }
            
            if(isset($_GET['q']) && $_GET['q']){
                $t = $_GET['st'];
                $t_title = $t == 1 ? '用户名' : '订单编号';
                $keyword = trim($_GET['q']);
                if($t == 1){
                    $user = $this->users_model->get_user_by_username($keyword);
                    if($user){
                        $where['withdraw_list.user_id'] = $user['user_id'];
                    }
                }else{
                    $where['order_nid'] = $keyword;
                }
            }
            
            if($_GET['time-start']){
                $where['create_time >='] = strtotime($_GET['time-start']);
            }
            if($_GET['time-end']){
                $where['create_time <='] = strtotime($_GET['time-end']);
            }
            
             //总数
            $total = $this->withdraw_list_model->get_count($where);
            $navigation_position = 'outside';
            $records_per_page = 10;
            $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position,
                array('left', 'right')) ? $navigation_position : 'outside');
            //$this->xpage->reverse(true);
            $this->xpage->records($total);
            $this->xpage->records_per_page($records_per_page);
            $this->xpage->labels("<<", ">>");
            //记录
            $list = $this->withdraw_list_model->get_list($where, (($this->xpage->get_page() - 1) * $records_per_page),
                $records_per_page);
            foreach ($list as $key => $val){
                $list[$key]['status'] = $val['status'] == 1 ? '已支付' : ($val['status'] == 2 ? '未支付' : '失败');
                $list[$key]['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
                $list[$key]['update_time'] = $val['update_time'] ? date('Y-m-d H:i:s',$val['update_time']) : '--|--';
            }
            $T_total = $this->withdraw_list_model->get_total_count($where);
            
            $this->template->assign('name', $keyword);
            $this->template->assign('cont1', $type);
            $this->template->assign('cont', $t_title);
            $this->template->assign('T_total', $T_total);
            $this->template->assign('Total', $total);
            $this->template->assign('time_start', $_GET['time-start']);
            $this->template->assign('time_end', $_GET['time-end']);
            $this->template->assign('showpage', $this->xpage->render(true));
            $this->template->assign('list', $list);
            $this->template->load('finance/withdraw');
        }
        //兑换明细
        public function exchange(){
            $where = array();
            
            //变动类型
            if(isset($_GET['stm']) && $_GET['stm']){
                $where['exchange_list.status'] = intval($_GET['stm']);
                $type = $_GET['stm'] == 1 ? '已兑换' : ($_GET['stm'] == 2 ? '未兑换' : '失败');
            }
            
            if(isset($_GET['q']) && $_GET['q']){
                $t = $_GET['st'];
                $t_title = $t == 1 ? '用户名' : '订单编号';
                $keyword = trim($_GET['q']);
                if($t == 1){
                    $user = $this->users_model->get_user_by_username($keyword);
                    if($user){
                        $where['exchange_list.user_id'] = $user['user_id'];
                    }
                }else{
                    $where['order_nid'] = $keyword;
                }
            }
            
            if($_GET['time-start']){
                $where['create_time >='] = strtotime($_GET['time-start']);
            }
            if($_GET['time-end']){
                $where['create_time <='] = strtotime($_GET['time-end']);
            }
            
             //总数
            $total = $this->exchange_list_model->get_count($where);
            $navigation_position = 'outside';
            $records_per_page = 10;
            $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position,
                array('left', 'right')) ? $navigation_position : 'outside');
            //$this->xpage->reverse(true);
            $this->xpage->records($total);
            $this->xpage->records_per_page($records_per_page);
            $this->xpage->labels("<<", ">>");
            //记录
            $list = $this->exchange_list_model->get_list($where, (($this->xpage->get_page() - 1) * $records_per_page),
                $records_per_page);
            foreach ($list as $key => $val){
                $list[$key]['status'] = $val['status'] == 1 ? '已兑换' : ($val['status'] == 2 ? '未兑换' : '失败');
                $list[$key]['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
                $list[$key]['update_time'] = $val['update_time'] ? date('Y-m-d H:i:s',$val['update_time']) : '--|--';
            }
            $T_total = $this->exchange_list_model->get_total_count($where);
            
            $this->template->assign('name', $keyword);
            $this->template->assign('cont1', $type);
            $this->template->assign('cont', $t_title);
            $this->template->assign('T_total', $T_total);
            $this->template->assign('Total', $total);
            $this->template->assign('time_start', $_GET['time-start']);
            $this->template->assign('time_end', $_GET['time-end']);
            $this->template->assign('showpage', $this->xpage->render(true));
            $this->template->assign('list', $list);
            
            $this->template->load('finance/exchange');
        }
        
    //溜币充值界面
    public function recharge()
    {	
        if($_POST){
            $username = isset($_POST['username']) ? trim($_POST['username']) : "";
            $amount = isset($_POST['amount']) ? trim($_POST['amount']) : 0;
            $user = $this->users_model->get_user_by_username($username);
            if(!$user){
                $mssage['info']	  = '不存在该用户';
                $mssage['status'] = 0;
                $mssage['url']	  = "";
                echo json_encode($mssage);exit;
            }
            
            if(!$amount || !preg_match("/^\\d+(\\.\\d+)?$/",$amount)){
                $mssage['info']	  = '充值金额格式非法';
                $mssage['status'] = 0;
                $mssage['url']	  = "";
                echo json_encode($mssage);exit;
            }
            
            //获取用户账户信息
            $account = $this->account_model->get($user['user_id']);
            $account_data = array();
            $amount = floatval($amount) * 10000;
                
            $account_data['balance'] = $account['balance'] + $amount;//账户余额
            $account_data['total'] = $account['total']+$amount;//总额金额
            //更新账户
            $this->account_model->update($account_data,$user['user_id']);

            //弹溜溜明细
            $finance_list_data = array();
            $finance_list_data['user_id'] = $user['user_id'];
            $finance_list_data['content'] = '线下充值'.$amount.'溜溜币';
            $finance_list_data['finance'] = $amount;
            $finance_list_data['finance_balance'] = $account_data['balance'];
            $finance_list_data['reward_type'] = 'spread_award';
            $finance_list_data['type'] = 1;//收入
            $finance_list_data['create_time'] = time();
            $finance_list_data['month'] = date("Ym");
            $finance_list_data['day'] = date("Ymd");
            $result = $this->finance_list_model->insert($finance_list_data);
            
            if($result){
                $mssage['info']	  = '充值成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('finance/recharge');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('finance/recharge');
                echo json_encode($mssage);exit;
            }
            
        }
        $this->template->load('finance/recharge');
    }
        
}
